﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.IO;
using SD = System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Configuration;

namespace WebAddressBook
{
    public static class CommonHelper
    {
        /// <summary>
        /// 获取企业列表
        /// </summary>
        /// <returns></returns>
        public static DataSet GetEnterprise()
        {
            string strWhere = "len(DeptID)=4 and isDelete='false' order by DeptID";
            //string strWhere = "isDelete='false' order by DeptID";           
            Adrr.BLL.Base_Dept baseDeptBll = new Adrr.BLL.Base_Dept();
            DataSet ds = baseDeptBll.GetList(strWhere);
            //DataRow[] dr = ds.Tables[0].Select("len(DeptID)=4");
            //DataTable dt = new DataTable();
            //for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            //{
            //    string deptId = ds.Tables[0].Rows[i]["DeptID"].ToString();
            //    string deptName = ds.Tables[0].Rows[i]["DeptName"].ToString();
            //}
            return ds;
        }

        public static void GetRecDept(string fatherdeptId, int len)
        {
            string strWhere = "DeptID like '" + fatherdeptId + "%' and len(DeptID)=" + len + " and isDelete='false' order by DeptID";
            Adrr.BLL.Base_Dept baseDeptBll = new Adrr.BLL.Base_Dept();
            DataSet ds = baseDeptBll.GetList(strWhere);
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                string deptId = ds.Tables[0].Rows[i]["DeptID"].ToString();
                string deptName = ds.Tables[0].Rows[i]["DeptName"].ToString();
            }
        }

        public static DataTable GetDeptList(string enterpriseId)
        {
            string strWhere = "DeptID like '" + enterpriseId + "%' and isDelete='false' and DeptID!='" + enterpriseId + "'";
            Adrr.BLL.Base_Dept baseDeptBll = new Adrr.BLL.Base_Dept();
            DataSet ds = baseDeptBll.GetList(strWhere);
            DataTable dt=new DataTable ();
            dt.Columns.Add("DeptID", typeof(System.String));//类型是可以变换的，比如System.Int32,System.Double..
            dt.Columns.Add("DeptName", typeof(System.String));
            //所有的一级部门节点
            DataRow[] drs = ds.Tables[0].Select("len(DeptID)=8");         
            foreach (DataRow dr in drs)
            {
                string deptId = dr["DeptID"].ToString();
                string deptName = dr["DeptName"].ToString();
                DataRow row=dt.NewRow();
                row["DeptID"]=deptId;
                row["DeptName"]=deptName;
                dt.Rows.Add(row);
                GetRecDepList(deptId, ds, dt, 12);
            }

            return dt;
        }
        /// <summary>
        /// 递归将部门组织成有层次的显示（前面补--）
        /// </summary>
        /// <param name="fatherDeptId"></param>
        /// <param name="ds"></param>
        /// <param name="dt"></param>
        /// <param name="len"></param>
        private static void GetRecDepList(string fatherDeptId,DataSet ds,DataTable dt,int len)
        {
            DataRow[] drs = ds.Tables[0].Select("DeptID like '"+fatherDeptId+"%' and len(DeptID)=" + len);
            foreach (DataRow dr in drs)
            {
                string deptId = dr["DeptID"].ToString();
                string deptName = dr["DeptName"].ToString();
                string str = "";
                for (int i = 0; i < len-6; i++)
                {
                    str += "-";
                }
                DataRow row = dt.NewRow();
                row["DeptID"] = deptId;
                row["DeptName"] =str+deptName;
                dt.Rows.Add(row);
                GetRecDepList(deptId, ds, dt, len+4);
            }
        }
        /// <summary>
        /// 获取相应企业id下的部门列表
        /// </summary>
        /// <param name="enterpriseId"></param>
        /// <returns></returns>
        public static DataSet GetDept(string enterpriseId)
        {
            string strWhere = "DeptID like '" + enterpriseId + "%' and isDelete='false' and DeptID!='" + enterpriseId + "'";
            Adrr.BLL.Base_Dept baseDeptBll = new Adrr.BLL.Base_Dept();
            DataSet ds = baseDeptBll.GetList(strWhere);
            return ds;
        }

        /// <summary>
        /// 接口数据服务地址
        /// </summary>
        public static string DataServerUrl {
            get {
                return 
                    ConfigurationManager.AppSettings["DataSourceServer"].ToString();
            }
        }
        /// <summary>
        /// 进行裁剪操作
        /// </summary>
        public static byte[] Crop(string Img, int Width, int Height, int X, int Y)
        {
            try
            {
                using (SD.Image OriginalImage = SD.Image.FromFile(Img))
                {
                    using (SD.Bitmap bmp = new SD.Bitmap(Width, Height, OriginalImage.PixelFormat))
                    {
                        bmp.SetResolution(OriginalImage.HorizontalResolution, OriginalImage.VerticalResolution);
                        using (SD.Graphics Graphic = SD.Graphics.FromImage(bmp))
                        {
                            Graphic.SmoothingMode = SmoothingMode.AntiAlias;
                            Graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
                            Graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
                            Graphic.DrawImage(OriginalImage, new SD.Rectangle(0, 0, Width, Height), X, Y, Width, Height, SD.GraphicsUnit.Pixel);
                            MemoryStream ms = new MemoryStream();
                            bmp.Save(ms, OriginalImage.RawFormat);
                            return ms.GetBuffer();
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
                throw (Ex);
            }
        }


        /// <summary>
        /// 把Copyright的相关信息配置在web.config里面，这个函数获取出来
        /// </summary>
        /// <returns>Copyright</returns>
        public static string GetCopyright()
        {
            try
            {
                string copyright = ConfigurationManager.AppSettings["Copyright"];
                string coptightTitle = ConfigurationManager.AppSettings["CopyrightTitle"];
                string copyrightLink = ConfigurationManager.AppSettings["CopyrightLink"];
                string companyName = ConfigurationManager.AppSettings["CompanyName"];
                string divHtmlInner = "<p>" + copyright + "  <b><a href=\"" + copyrightLink + "\" title=\"" + coptightTitle + "\">" + companyName + "</a></b></p>";
                return divHtmlInner;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}